package com.p6spy.enginex.logging;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;

@SuppressWarnings("unchecked")
public class P6LogConnectionProxy implements InvocationHandler {
	private Connection conn;

	public P6LogConnectionProxy() {
	}

	public P6LogConnectionProxy(Connection conn) {
		super();
		this.conn = conn;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("before save");
		Object o = method.invoke(conn, args);
		System.out.println("after save");
		return o;
	}

	public static Connection proxy(Connection real) {
		Class cls = real.getClass();
		return (Connection)Proxy.newProxyInstance(cls.getClassLoader(),
				cls.getInterfaces(), new P6LogConnectionProxy(real));
	}
}
